// 创建角色相关的小仓库
import { defineStore } from "pinia";
// 引入接口
import { reqRoleList, reqAddRole, reqUpdateRole } from "@/api/role";

// 引入status返回的数据类型
import type { RoleState } from "./types/types";
// 引入数据类型

import type { roleInforDate, getRoleListResponseData, addRoleResponseData } from "@/api/role/type";
// 创建角色小仓库
let useRoleStore = defineStore("Role", {
    state: (): RoleState => {
        return {
            roles: []
        }
    },
    actions: {
        async getRoleList() {
            // 调用api中的方法发生ajax请求
            const result: getRoleListResponseData = await reqRoleList()
            if (result.status == 0) {
                // @ts-ignore
                this.roles = result.data as roleInforDate[]
            

                return "ok"
            } else {
            

                return Promise.reject(new Error(result.msg))
            }
        },
        async addRole(data: roleInforDate) {


            const result: addRoleResponseData = await reqAddRole(data);
            if (result.status == 0) {
                this.getRoleList()
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 设置角色权限
        async updateRole(data: roleInforDate) {

            const result: addRoleResponseData = await reqUpdateRole(data);
            if (result.status == 0) {
                this.getRoleList()
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        
    }
})
export default useRoleStore